class UserAsset < ActiveRecord::Base

  def validate
    errors.add("image_size", "Image is too large, cannot be larger than 100x100.") if ( (self.width > 100) || (self.height > 100) )
    errors.add("invalid_type", "File is the wrong format. (JPEG, GIF, AND PNG ONLY)") unless ( ( self.content_type == 'image/jpeg' ) || ( self.content_type == 'image/gif' ) || ( self.content_type == 'image/png' ) )
  end

  def image_file=(input_data)
    original_filename = input_data.original_filename.split(".")
    self.filename = original_filename[0].to_s
    self.ext = original_filename[1].to_s
    self.content_type = input_data.content_type.chomp
    self.binary_data = input_data.read
    if self.content_type == 'image/jpeg'
      file = JPEG.new(input_data.path)
      self.width = file.width
      self.height = file.height
    elsif self.content_type == 'image/gif'
      dim = IO.read(input_data.path)[6..10].unpack('SS')
      self.width = dim[0]
      self.height = dim[1]
    elsif self.content_type == 'image/png'
      dim = IO.read('image.png')[0x10..0x18].unpack('NN')
      self.width = dim[0]
      self.height = dim[1]
    else
      self.width = 0
      self.height = 0
    end
  end

end
